Skip to content

Method: addNewNodes(SimpleListValueDescriptor, int, NamedResource)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi.list;
16:
17: import cz.cvut.kbss.ontodriver.owlapi.OwlapiAdapter;
18: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
19: import cz.cvut.kbss.ontodriver.owlapi.util.MutableAddAxiom;
20: import cz.cvut.kbss.ontodriver.descriptor.ListDescriptor;
21: import cz.cvut.kbss.ontodriver.descriptor.SimpleListDescriptor;
22: import cz.cvut.kbss.ontodriver.descriptor.SimpleListValueDescriptor;
23: import cz.cvut.kbss.ontodriver.model.Assertion;
24: import cz.cvut.kbss.ontodriver.model.AxiomImpl;
25: import cz.cvut.kbss.ontodriver.model.NamedResource;
26: import cz.cvut.kbss.ontodriver.model.Value;
27: import org.semanticweb.owlapi.model.OWLAxiom;
28: import org.semanticweb.owlapi.model.OWLOntologyChange;
29:
30: import java.util.ArrayList;
31: import java.util.List;
32:
33: class SimpleListHandler extends ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> {
34:
35: SimpleListHandler(OwlapiAdapter adapter, OntologySnapshot snapshot) {
36: super(adapter, snapshot);
37: }
38:
39: @Override
40: OwlapiListIterator iterator(ListDescriptor descriptor) {
41: if (descriptor.getListProperty().isInferred() || descriptor.getNextNode().isInferred()) {
42: return new InferredSimpleListIterator(descriptor, snapshot, axiomAdapter);
43: } else {
44: return new SimpleListIterator(descriptor, snapshot, axiomAdapter);
45: }
46: }
47:
48: @Override
49: List<OWLOntologyChange> createListAxioms(SimpleListValueDescriptor descriptor) {
50: final List<OWLOntologyChange> changes = new ArrayList<>(descriptor.getValues().size());
51: NamedResource previous = descriptor.getListOwner();
52: boolean first = true;
53: for (NamedResource item : descriptor.getValues()) {
54: final OWLAxiom axiom;
55: if (first) {
56: axiom = appendNode(previous, descriptor.getListProperty(), item);
57: first = false;
58: } else {
59: axiom = appendNode(previous, descriptor.getNextNode(), item);
60: }
61: previous = item;
62: changes.add(new MutableAddAxiom(ontology, axiom));
63: }
64: return changes;
65: }
66:
67: private OWLAxiom appendNode(NamedResource current, Assertion property, NamedResource next) {
68: return axiomAdapter.toOwlObjectPropertyAssertionAxiom(new AxiomImpl<>(current, property, new Value<>(next)));
69: }
70:
71: @Override
72: boolean isOrigEmpty(SimpleListValueDescriptor descriptor) {
73: final OwlapiListIterator it = iterator(descriptor);
74: return !it.hasNext();
75: }
76:
77: @Override
78: void addNewNodes(SimpleListValueDescriptor descriptor, int index, NamedResource lastNode) {
79:• if (index >= descriptor.getValues().size()) {
80: return;
81: }
82: final List<OWLOntologyChange> changes = new ArrayList<>(descriptor.getValues().size() - index);
83:• for (; index < descriptor.getValues().size(); index++) {
84: final NamedResource next = descriptor.getValues().get(index);
85: changes.add(new MutableAddAxiom(ontology, appendNode(lastNode, descriptor.getNextNode(), next)));
86: lastNode = next;
87: }
88: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(changes));
89: }
90: }